package uiLayer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import errorLayer.*;
import modelLayer.Subject;
import controlLayer.SubjectCtrl;

public class SubjectGUI extends JPanel {
	
	private JTextField subjectIDField = new JTextField();
	private JTextField subjectNameField = new JTextField();
	private JComboBox allSubjectsBox = new JComboBox();
	private List<Subject> allSubjects = new ArrayList<>();
	private SubjectCtrl sc = new SubjectCtrl();
	private JButton addBtn = new JButton("Add");
	private JButton editBtn = new JButton("Edit");
	private JButton removeBtn = new JButton("Remove");
	
	public SubjectGUI () {
		
		setLayout(new BorderLayout(0, 0));
		
		allSubjects = sc.allSubjects();
		
		setup();
		
	}
	
	private void setup() {
		
		JPanel subjectContent = new JPanel();
		subjectContent.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Subject"));
		subjectContent.setLayout(new BorderLayout(0, 0));
		add(subjectContent, BorderLayout.NORTH);
		
		JPanel textNameArea = new JPanel();
		textNameArea.setLayout(new GridLayout(4, 1, 0, 5));
		subjectContent.add(textNameArea, BorderLayout.WEST);
		
		JPanel textArea = new JPanel();
		textArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		textArea.setLayout(new GridLayout(4, 1, 0, 5));
		subjectContent.add(textArea, BorderLayout.CENTER);
		
		JPanel buttonArea = new JPanel();
		buttonArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonArea.setLayout(new GridLayout(4, 1, 0, 5));
		subjectContent.add(buttonArea, BorderLayout.EAST);
		
		subjectIDField.setEditable(false);
		
		allSubjectsBox.addItem("New subject");
		
		if (allSubjects.size() == 0) {
			
			allSubjectsBox.addItem("No subjects");
			
		}
		
		for (int i = 0; i < allSubjects.size(); ++i) {
			
			allSubjectsBox.addItem(allSubjects.get(i));
			
		}
		
		allSubjectsBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if (e.getItem() == "New subject") {

					addBtn.setEnabled(true);
					editBtn.setEnabled(false);
					removeBtn.setEnabled(false);
					
					subjectIDField.setText("");
					subjectNameField.setText("");
					
				}
				else if (e.getItem() != "New subject") {

					addBtn.setEnabled(false);
					editBtn.setEnabled(true);
					removeBtn.setEnabled(true);
					
					try {
						
						Subject subject = (Subject) e.getItem();
						
						subjectIDField.setText("" + subject.getID());
						subjectNameField.setText(subject.getName());
						
					}
					catch (Exception ex) {
						System.out.println("Exception: " + ex);
					}
					
				}
				
			}
		});
		
		buttonArea.add(allSubjectsBox);
		
		addBtn.setPreferredSize(new Dimension(150, 20));
		addBtn.setMinimumSize(new Dimension(150, 20));
		addBtn.setMaximumSize(new Dimension(150, 20));
		
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Subject subject = new Subject();
				
				if (!subjectNameField.getText().isEmpty()) {
					
					subject.setName(subjectNameField.getText());
					
					sc.addSubject(subject);
					
					if (allSubjectsBox.getItemAt(1) == "No subjects") {
						allSubjectsBox.remove(1);
					}
					
					subject.setID(sc.getHighestID());
					
					allSubjectsBox.addItem(subject);
					allSubjectsBox.setSelectedIndex((allSubjectsBox.getItemCount() - 1));
					
				}
				else {
					new EmptyField("Subject name");
				}
				
			}
		});
		
		buttonArea.add(addBtn);
		
		editBtn.setPreferredSize(new Dimension(150, 20));
		editBtn.setMinimumSize(new Dimension(150, 20));
		editBtn.setMaximumSize(new Dimension(150, 20));
		editBtn.setEnabled(false);
		
		editBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Subject subject = new Subject();
				
				if (!subjectIDField.getText().isEmpty()) {
					
					subject.setID(Integer.parseInt(subjectIDField.getText()));
					subject.setName(subjectNameField.getText());
					
					sc.updateSubject(subject);
					
					allSubjectsBox.removeItemAt(allSubjectsBox.getSelectedIndex());
					allSubjectsBox.addItem(subject);
					allSubjectsBox.setSelectedItem((allSubjectsBox.getItemCount() - 1));
					
				}
				
			}
		});
		
		buttonArea.add(editBtn);
		
		removeBtn.setPreferredSize(new Dimension(150, 20));
		removeBtn.setMinimumSize(new Dimension(150, 20));
		removeBtn.setMaximumSize(new Dimension(150, 20));
		removeBtn.setEnabled(false);
		
		removeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				String deletion = "Subject: " + subjectIDField.getText() + ", " + subjectNameField.getText();
				
				DeleteWarning dw = new DeleteWarning(deletion);
				
				if (dw.getAnswer()) {
					sc.removeSubject(Integer.parseInt(subjectIDField.getText()));
				
					allSubjectsBox.removeItemAt(allSubjectsBox.getSelectedIndex());
				}
				
			}
		});
		
		buttonArea.add(removeBtn);
		
		textNameArea.add(new JLabel("ID"));
		textNameArea.add(new JLabel("Name"));
		
		textArea.add(subjectIDField);
		textArea.add(subjectNameField);
		
	}

}
